package com.zhang.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.common.dto.MenuDto;
import com.zhang.common.lang.Result;
import com.zhang.entity.SysMenu;
import com.zhang.entity.SysRoleMenu;
import com.zhang.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张立松
 * @since 2022-12-22
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController{

    //获取用户的菜单和权限信息接口
    @GetMapping("/nav")
    public Result getNav(Principal principal){ //Principal为Security内置的，可通过它获取登录用户的信息
        SysUser user = sysUserService.getByUsername(principal.getName());

        //获取权限信息
        String authorityInfo = sysUserService.getAuthorityInfo(user.getId());
        String[] authoritys = authorityInfo.split(",");

        //获取导航栏信息
        List<MenuDto> nav = sysMenuService.getUserNav(user);

        //用工具类构建Map集合返回结果
        return Result.succ(MapUtil.builder().put("authoritys",authoritys).put("nav",nav).map());
    }

    //获取单个菜单信息接口
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result getMenuInfo(@PathVariable(name = "id") Long id){
        return Result.succ(sysMenuService.getById(id));
    }

    //获取菜单列表
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result getMenuList(){
        return Result.succ(sysMenuService.getMenuTreeList());
    }

    //保存新增的菜单信息
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public Result saveMenuInfo(@Validated @RequestBody SysMenu menu){    //加了@Validated注解，validation工具包会进行校验
        menu.setCreated(LocalDateTime.now());
        sysMenuService.save(menu);
        return Result.succ(menu);
    }

    //更新修改的菜单信息
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result updateMenuInfo(@Validated @RequestBody SysMenu menu){ //加了@Validated注解，validation工具包会进行数据校验
        menu.setUpdated(LocalDateTime.now());
        sysMenuService.updateById(menu);
        //★★菜单被修改一定要清除与此菜单相关的用户权限缓存以便更新权限
        sysUserService.clearUserAuthorityInfoByMenuId(menu.getId());
        return Result.succ(menu);
    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result deleteMenuInfo(@PathVariable("id") Long id){
        //先判断有没有子菜单，有的话不能删除
        int count = sysMenuService.count(new QueryWrapper<SysMenu>().eq("parent_id", id));
        if(count > 0){
            return Result.fail("请先删除子菜单！");
        }
        sysMenuService.removeById(id);
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("menu_id",id));    //中间表相关的也要删除

        //★★菜单被删除一定要清除与此菜单相关的用户权限缓存以便更新权限
        sysUserService.clearUserAuthorityInfoByMenuId(id);

        return Result.succ("删除成功");
    }
}

